var rootPath = getRootPath();

$(document).ready(function(){
	var userId = getQueryParamValue("userId");
	$("#userId").val(userId);
	var headImgurl = getQueryParamValue("headImgurl");
	$("#headImgurl").attr('src',headImgurl);
	var date = getQueryParamValue("date");
	$("#date").html(date+'天后');
	
	$.ajax({
        type:"GET", 
        url:rootPath+"/wechat/api/maintain/getBindCarInfos", 
        dataType:"json",      
        async: false,
        contentType:"application/json",               
        data:null, 
        success:function(data){ 
        	if (data.length==0){
        		alert("您还没有绑定车辆，请先绑定车辆。");
        		/*window.location = rootPath + "/views/vehicleBinding/vehicleBinding.html";*/
        	}
            // 编辑轮播信息数据
			var wholeHtml = '';
			// 轮播（Carousel）指标
			var carousel_indicatorsHtml = '<ol class="carousel-indicators">';
			
			// 轮播（Carousel）项目
			var carousel_innerHtml = '<div class="carousel-inner">';
			
			
			$.each(data,function(carModelIndex,carModelObj){
				if (0 == carModelIndex){
					
//				    carousel_indicatorsHtml+= '<li data-target="#myCarInfos" data-slide-to='+carModelIndex+' class="active"></li>';
					carousel_indicatorsHtml += '<li class="active"><a href="#" class="active" onclick=setVin('+carModelIndex+')><img alt="" src="'+rootPath+'/images/'+carModelObj.brandName+'/外观/'+carModelObj.seriesName+'/'+carModelObj.seriesName+'.jpg'+'"  data-target="#myCarInfos" data-slide-to="'+ carModelIndex +'" class="img-responsive"/ ></a></li>';
				    carousel_innerHtml += "<div class='item active'>";
				    //品牌CODE赋值
				    $("#vin").val(carModelObj.vin);
				} else {
//					carousel_indicatorsHtml+= '<li data-target="#myCarInfos" data-slide-to='+carModelIndex+' ></li>';
					carousel_indicatorsHtml += '<li><a href="#" onclick=setVin('+carModelIndex+')><img alt="" src="'+rootPath+'/images/'+carModelObj.brandName+'/外观/'+carModelObj.seriesName+'/'+carModelObj.seriesName+'.jpg'+'"  data-target="#myCarInfos" data-slide-to="'+carModelIndex+'" class="img-responsive"/ ></a></li>';
					carousel_innerHtml += "<div class='item'>";
				}
				
				carousel_innerHtml += '<table><tr>';
				carousel_innerHtml +="<td nowrap='nowrap'><div class=''>";
				carousel_innerHtml += "<p>";
				carousel_innerHtml +="<h5  style='font: bolder;color: #000;'>"+carModelObj.registNo+"</h5>";
				carousel_innerHtml +="<span class='text-muted'>"+carModelObj.seriesName+"</span>";
				carousel_innerHtml +="</div></td>"
				carousel_innerHtml +='<td><img src="'+rootPath+'/images/'+carModelObj.brandName+'/外观/'+carModelObj.seriesName+'/'+carModelObj.seriesName+'.jpg'+'" width="140" height="60">'
				carousel_innerHtml +="</td></tr></table>";
				carousel_innerHtml +="<input type='hidden' id = 'vin_"+carModelIndex+"' value='"+carModelObj.vin+"' /></div>";
			});
			
			carousel_indicatorsHtml += "</ol> ";
			carousel_innerHtml += "</div>";
			
			// 轮播（Carousel）导航
//			var carousel_controlHtml = '<a class="carousel-control left"  href="#myCarInfos"  data-slide="prev" type = "button">&lsaquo;</a>';
//			    carousel_controlHtml += '<a class="carousel-control right" href="#myCarInfos"  data-slide="next" type = "button">&rsaquo;</a>';
//			
			$("#myCarInfos").append(carousel_indicatorsHtml);
			$("#myCarInfos").append(carousel_innerHtml);
			
			// 停止自动轮播
			$('#myCarInfos').carousel({
				interval: false
			})
			// 停止自动轮播
			$('#myCarInfoslinks').carousel({
				interval: false
			})
			$("#myCarInfos").carousel('pause');
			$("#myCarInfoslinks").carousel('pause');
			
/////////////////////////////////////
			
			$('#myCarInfos').hammer().on('swipeleft', function(){
				$(this).carousel('next');
			});
			
			$('#myCarInfos').hammer().on('swiperight', function(){
				$(this).carousel('prev');
			});

////////////////////////////////////

			var vin = $("#vin").val();
			$.ajax({
				method : 'get',
				url : rootPath+'/wechat/api/potentialUsers/maintainRecord/lastMaintain?userId='+userId+'&vin='+vin, 
				error:function(XMLHttpRequest, ajaxOptions, thrownError){
					alert("连接服务器错误!");
				},
				success : function(resultData){

					var registNo = ''; 	
					var modelName = ''; 	
					var lastMaintainDate = ''; 
					var lastMaintainMileage = '';
					var nextMaintainMileage = '';
					
					$.each(resultData,function(lastMaintainIndex,lastMaintainObj){

						registNo = lastMaintainObj.registNo;
						modelName = lastMaintainObj.modelName;
						lastMaintainDate = lastMaintainObj.lastMaintainDate;
						lastMaintainMileage = lastMaintainObj.lastMaintainMileage;
						nextMaintainMileage = lastMaintainMileage + 20000;
					});

					$("#registNo").html(registNo);
					$("#carName").html(modelName);
					$("#lastMaintainDate").html(lastMaintainDate);
					$("#lastMaintainMileage").html(lastMaintainMileage);
					$("#nextMaintainMileage").html(nextMaintainMileage);
					$("#da").html(da+'天');
				}
			});

        },
        error:function(data){
        	alert("数据刷新失败，请联系管理员！");
        }
     });
     
     
	/**
	 * 设置选择的车型
	 * @param index
	 * @returns
	 */
	function setVin(index){
		$("#vin").val($("#vin_"+index).val());
	}
	
	/**
	 * 监听轮播信息
	 */
	$(function(){
	    $('#myCarInfos').on('slid.bs.carousel', function (e) {
	    	 var active = $(e.target).find('.carousel-inner > .item.active');
	    	 var ccindex = active.index();
	    	  $("#vin").val($("#vin_"+ccindex).val());
	    });
	});
	
	
	/**
	 * 设置选择的车型
	 * @param index
	 * @returns
	 */
	function setVin(index){
		$("#vin").val($("#vin_"+index).val());
	}
	
	/**
	 * 监听轮播信息
	 */
	$(function(){
	    $('#myCarInfos').on('slid.bs.carousel', function (e) {
	    	 var active = $(e.target).find('.carousel-inner > .item.active');
	    	 var ccindex = active.index();
	    	  $("#vin").val($("#vin_"+ccindex).val());
	    });
	});

})